package ge.gen.eBdgt.action;

import ge.gen.eBdgt.action.graph.IcnTeamWrkLdGraph;
import ge.gen.eBdgt.vo.IcnVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class IcnSearchAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private String icnSearch;
	private String descrSearch;
	private String statusSearch;
	private String stat;
	private String reqmgr;
	private String searchValue;
	protected List<IcnVO> icnVOSearchList;
	private String defaultSearchValue;
	private List<SearchCriterias> searchCrit;
	private String searchSel;
	private HashMap<String, String> selectionMap;
	private String searchLbl1;
	private String searchValue1;
	private String searchValue2;
	private String searchValue3;
	
	public String getSearchLbl1() {
		return searchLbl1;
	}

	public void setSearchLbl(String searchLbl) {
		this.searchLbl1 = searchLbl;
	}

	public IcnSearchAction() throws Exception {
		icnVOSearchList = icnVOList;
		searchCrit = new ArrayList<SearchCriterias>();
		selectionMap = new HashMap<String, String>();
		searchCrit.add(setSearchCriterias("icn", "ICN"));
		searchCrit.add(setSearchCriterias("cust", "CUSTOMER"));
		searchCrit.add(setSearchCriterias("sttn", "STATION"));
		searchCrit.add(setSearchCriterias("ser", "SERIAL NUMBER"));
		searchCrit.add(setSearchCriterias("status", "STATUS"));
		searchCrit.add(setSearchCriterias("req", "REQ ENG"));
		//searchCrit.add(setSearchCriterias("notes", "NOTES"));
		searchCrit.add(setSearchCriterias("mnscp", "MAIN SCOPE"));
		searchCrit.add(setSearchCriterias("addscp", "ADD SCOPE"));
		searchLbl1 = "SEARCH ICN";
		if (isBlank(searchSel))
			defaultSearchValue = "icn";
		else
			defaultSearchValue = searchSel;
	}

	private SearchCriterias setSearchCriterias(String id, String name) {
		selectionMap.put(id, name);
		return new SearchCriterias(id, name);

	}

	public String execute() throws Exception {
		return "search";
	}

	public String getDefaultSearchValue() {
		return defaultSearchValue;
	}

	public void setDefaultSearchValue(String defaultSearchValue) {
		this.defaultSearchValue = defaultSearchValue;
	}

	public String search() {
		session = request.getSession();

		try {
			String selectDesc = selectionMap.get(searchSel).toUpperCase();
			defaultSearchValue = searchSel;
			searchLbl1 = "SEARCH " + selectDesc;
			if (searchSel.equals("status")) {
				searchValue = searchValue1.trim();
			} else if (searchSel.equals("mnscp")) {
				searchValue = searchValue3.trim();
			} 
			if (isBlank(searchValue)) {
				addActionError(selectDesc + " : Search Value is Empty");
				return SUCCESS;
			} else {
				searchValue = searchValue.toUpperCase();
			}
			if (searchSel.equals("notes"))
				icnVOSearchList = icnService.searchByDescr(searchValue);
			else if (searchSel.equals("status")) {
				icnVOSearchList = icnService.searchByStatus(searchValue);
			} else if (searchSel.equals("icn")) {
				icnVOSearchList = icnService.searchByIcn(searchValue);
			} else if (searchSel.equals("cust")) {
				icnVOSearchList = icnService.searchByCustomer(searchValue);
			} else if (searchSel.equals("addscp")) {
				icnVOSearchList = icnService.searchByAddlScope(searchValue);
			} else if (searchSel.equals("req")) {
				icnVOSearchList = icnService.searchByReqEngr(searchValue);
			} else if (searchSel.equals("mnscp")) {
				icnVOSearchList = icnService.searchByMainScope(searchValue);
			} else if (searchSel.equals("ser")) {
				icnVOSearchList = icnService.searchBySerNum(searchValue);
			} else if (searchSel.equals("sttn")) {
				icnVOSearchList = icnService.searchByStation(searchValue);
			} else
				icnVOSearchList = icnVOList;
			request.getSession().setAttribute("searchSel", searchSel);
			request.getSession().setAttribute("icnListMap", getIcnListMap());
			request.getSession().setAttribute("icnVOSearchList",
					icnVOSearchList);
		} catch (Exception e) {

			e.printStackTrace();
		}
		return SUCCESS;
	}

	public void validate() {
		if (isNotNull(descrSearch) && isNotNull(icnSearch)) {
			addActionError("Both Description and ICN are mutually exclusive!!");
		}
	}

	public String getDescrSearch() {
		return descrSearch;
	}

	public void setDescrSearch(String descrSearch) {
		this.descrSearch = descrSearch;
	}

	public String getIcnSearch() {
		return icnSearch;
	}

	public void setIcnSearch(String icnSearch) {
		this.icnSearch = icnSearch;
	}

	public List<IcnVO> getIcnVOSearchList() {
		return icnVOSearchList;
	}

	public void setIcnVOSearchList(List<IcnVO> icnVOSearchList) {
		this.icnVOSearchList = icnVOSearchList;
	}

	public String getStatusSearch() {
		return statusSearch;
	}

	public void setStatusSearch(String statusSearch) {
		this.statusSearch = statusSearch;
	}

	public List<SearchCriterias> getSearchCrit() {
		return searchCrit;
	}

	public void setSearchCriterias(List<SearchCriterias> searchCrit) {
		this.searchCrit = searchCrit;
	}

	public String getSearchSel() {
		return searchSel;
	}

	public void setSearchSel(String searchSel) {
		this.searchSel = searchSel;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue) {
		this.searchValue = searchValue;
	}

	public String getStat() {
		return stat;
	}

	public void setStat(String stat) {
		this.stat = stat;
	}

	public String getSearchValue1() {
		return searchValue1;
	}

	public void setSearchValue1(String searchValue1) {
		this.searchValue1 = searchValue1;
	}

	public String getSearchValue2() {
		return searchValue2;
	}

	public void setSearchValue2(String searchValue2) {
		this.searchValue2 = searchValue2;
	}

	public String getSearchValue3() {
		return searchValue3;
	}

	public void setSearchValue3(String searchValue3) {
		this.searchValue3 = searchValue3;
	}

	public String getReqmgr() {
		return reqmgr;
	}

	public void setReqmgr(String reqmgr) {
		this.reqmgr = reqmgr;
	}

	public void setSearchCrit(List<SearchCriterias> searchCrit) {
		this.searchCrit = searchCrit;
	}
}

class SearchCriterias {
	private String searchCode;
	private String searchCriteria;

	public String getSearchCode() {
		return searchCode;
	}

	public void setSearchCode(String searchCode) {
		this.searchCode = searchCode;
	}

	public String getSearchCriteria() {
		return searchCriteria;
	}

	public void setSearchCriteria(String searchCriteria) {
		this.searchCriteria = searchCriteria;
	}

	public SearchCriterias(String searchCode, String searchCriteria) {
		this.searchCode = searchCode;
		this.searchCriteria = searchCriteria;
	}
}